# -*- coding:utf-8 -*-

import os
import unittest
from time import sleep

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from AutoTestBDC.WorkFlow.User import User
from AutoTestBDC.WorkFlow.WorkFlow import WorkFlow
from AutoTestBDC.extScript.Excel import RdExcel
from AutoTestBDC.extScript.ParseBDCType import BDCType
from AutoTestBDC.extScript.ReadTextFile import ReadTestFile
from AutoTestBDC.extScript.UploadPic import UploadPic


class ZRR(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.browertype = "firefox"

        # iedriver = os.getcwd() + "\\IEDriverServer.exe"
        # os.environ["webdriver.ie.driver"] = iedriver
        # self.driver = webdriver.Ie(executable_path=iedriver)
        # self.browertype = "ie"

        self.driver.maximize_window()
        self.driver.implicitly_wait(10)
        self.base_url = "http://192.168.200.248:8080"
        self.verificationErrors = []
        self.accept_next_alert = True

        self.casedate = RdExcel()
        self.caselist = self.casedate.getUserCaseByExcel("自然人")

        self.driver.get(self.base_url + "/RealestateTrade")
        User().Login(self.driver)
        self.dbt = BDCType()

    def test_zrrtester(self):
        driver = self.driver
        list = self.caselist

        # i = 7
        # for count in range(0,i-1):
        #     del list[0]

        for dist in list:
            # sleep(1)
            # dist = list[6]
            try:
                driver.find_element_by_xpath("/html/body/header/div[2]/ul[1]/li[1]/a").click()
            except Exception as e:
                sleep(2)
                driver.find_element_by_xpath("/html/body/header/div[2]/ul[1]/li[1]/a").click()

            self.zrr(driver, dist)

    def zrr(self, driver, dist):
        # 选择委托类型
        try:
            driver.find_element_by_link_text(dist["委托类型"]).click()
        except:
            driver.find_element_by_xpath("/html/body/header/div[2]/ul[1]/li[1]/a").click()
            sleep(1)
            self.movedownPage(driver, elementXPATH="/html/body/content/div/div[3]/div[2]/div[2]/ul/li[3]/a/h2")
            driver.find_element_by_link_text(dist["委托类型"]).click()

        if dist["代办"] == "是":
            driver.find_element_by_xpath("/html/body/content/div/form/div/div[2]/div[3]/input[1]").click()
        else:
            driver.find_element_by_xpath("/html/body/content/div/form/div/div[2]/div[3]/input[2]").click()

        Select(driver.find_element_by_id("entrustedPersons")).select_by_visible_text(dist["权利人类型"])
        driver.find_element_by_id("propertyOwner").clear()

        strname = dist["权利人姓名"] + "_" + str(ReadTestFile().readfile())

        driver.find_element_by_id("propertyOwner").send_keys(strname)
        driver.find_element_by_id("propertyIdCard").clear()
        driver.find_element_by_id("propertyIdCard").send_keys(int(dist["权利人身份证号"]))

        # 身份证是否归档
        if dist["身份证复印件"] == "归档":
            driver.find_element_by_id("copy1").click()

        UploadPic(driver, "/html/body/content/div/form/div/div[6]/div[2]/div[3]/div[1]/div[1]/div[2]/div"
                  , dist["身份证电子件"],self.browertype).upfile(
            "/html/body/content/div/form/div/div[6]/div[2]/div[3]/div[1]/div[1]/div[2]/a")

        driver.find_element_by_id("contactMobile").clear()
        driver.find_element_by_id("contactMobile").send_keys(int(dist["联系手机"]))
        driver.find_element_by_id("contactPhone").clear()
        driver.find_element_by_id("contactPhone").send_keys(dist["联系电话"])

        # 电子邮件 邮编
        driver.find_element_by_xpath("(//input[@type='text'])[11]").clear()
        driver.find_element_by_xpath("(//input[@type='text'])[11]").send_keys(dist["电子邮件"])
        driver.find_element_by_xpath("(//input[@type='text'])[12]").clear()
        driver.find_element_by_xpath("(//input[@type='text'])[12]").send_keys(int(dist["邮编"]))

        Select(driver.find_element_by_xpath(
            "/html/body/content/div/form/div/div[6]/div[9]/div[3]/span[1]/select")).select_by_visible_text(
            dist["联系地址-省"])
        sleep(1)
        Select(driver.find_element_by_xpath(
            "/html/body/content/div/form/div/div[6]/div[9]/div[3]/span[2]/select")).select_by_visible_text(
            dist["联系地址-市"])
        sleep(1)
        Select(driver.find_element_by_xpath(
            "/html/body/content/div/form/div/div[6]/div[9]/div[3]/span[3]/select")).select_by_visible_text(
            dist["联系地址-区"])
        sleep(1)
        driver.find_element_by_id("street1").clear()
        driver.find_element_by_id("street1").send_keys(dist["联系地址-街"])
        driver.find_element_by_id("streetNo1").clear()
        driver.find_element_by_id("streetNo1").send_keys(str(dist["联系地址-号"]))

        self.movedownPage(driver, elementID="contactEmail")

        # 添加委托人信息
        if dist["代办"] == "是":
            driver.find_element_by_id("depositaryName").send_keys(dist["受托人姓名S"])
            sleep(2)
            driver.find_element_by_id("depositaryIdCard").send_keys(int(dist["受托人身份证号S"]))

            # 添加身份证信息
            driver.find_element_by_xpath('/html/body/content/div/form/div/div[12]/div[3]/div[3]/input').click()

            UploadPic(driver, "/html/body/content/div/form/div/div[12]/div[4]/div[3]/div[1]/div[1]/div[2]/div"
                      , dist["身份证电子件S"],self.browertype).upfile(
                "/html/body/content/div/form/div/div[12]/div[4]/div[3]/div[1]/div[1]/div[2]/a")

            driver.find_element_by_xpath('/html/body/content/div/form/div/div[12]/div[6]/div[3]/input').send_keys(
                int(dist["联系手机S"]))
            driver.find_element_by_xpath('/html/body/content/div/form/div/div[12]/div[7]/div[3]/input').send_keys(
                dist["联系电话S"])
            driver.find_element_by_xpath('/html/body/content/div/form/div/div[12]/div[8]/div[3]/input').send_keys(
                dist["电子邮件S"])
            # driver.find_element_by_xpath('/html/body/content/div/form/div/div[12]/div[9]/div[3]/input').send_keys(dist["邮编S"])


            Select(driver.find_element_by_xpath(
                '/html/body/content/div/form/div/div[12]/div[11]/div[3]/span[1]/select')).select_by_visible_text(
                dist["联系地址-省S"])
            sleep(1)
            Select(driver.find_element_by_xpath(
                '/html/body/content/div/form/div/div[12]/div[11]/div[3]/span[2]/select')).select_by_visible_text(
                dist["联系地址-市S"])
            sleep(1)
            Select(driver.find_element_by_xpath(
                '/html/body/content/div/form/div/div[12]/div[11]/div[3]/span[3]/select')).select_by_visible_text(
                dist["联系地址-区S"])

            driver.find_element_by_xpath('/html/body/content/div/form/div/div[12]/div[12]/div[3]/input').send_keys(
                dist["联系地址-街S"])
            driver.find_element_by_xpath('/html/body/content/div/form/div/div[12]/div[12]/div[5]/input').send_keys(
                str(dist["联系地址-号S"]))

        driver.find_element_by_xpath('/html/body/content/div/form/div/div[13]/div[3]/textarea').clear()
        driver.find_element_by_xpath('/html/body/content/div/form/div/div[13]/div[3]/textarea').send_keys(dist["备注"])

        self.movedownPage(driver, elementID="next-step1")
        # driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_id("next-step1"))

        try:
            driver.find_element_by_id("next-step1").click()
        except Exception as e1:
            print(e1)

        # 不动产信息
        self.dbt.changeGotoInfoMessage(driver, dist["不动产类型"], dist["不动产登记类型用例"])

        # 不动产类型信息
        self.dbt.changeGotoWTType(driver, dist["委托类型"], dist["委托类型用例"])

        sleep(1)

        User().Logout(self.driver)

        # driver.find_element_by_xpath("/html/body/header/div[2]/div/ul/li[3]/a").click()
        # 走各种流程
        WorkFlow(self.driver, ReadTestFile().get_danganno()[3:], "自然人")
        User().Login(self.driver)

    # 封装一下，支持两种传入，一种是位置，另一种是控件显示
    def movedownPage(self, driver, lineNo=None, elementID=None, elementXPATH=None):
        if driver == "":
            driver = self.driver

        if lineNo is not None:
            js = "var q=document.documentElement.scrollTop=" + str(lineNo)
            driver.execute_script(js)

        if lineNo is None and elementID is not None:
            driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_id(elementID))

        if elementXPATH is not None:
            driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_xpath(elementXPATH))

        driver.implicitly_wait(5)


if __name__ == "__main__":
    unittest.main()
